package com.zhonghui.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhonghui.common.core.result.FwResult;
import com.zhonghui.common.core.utils.RedisUtils;
import com.zhonghui.dto.PageDto;
import com.zhonghui.dto.ProductCommentDto;
import com.zhonghui.mapper.ProductCommentMapper;
import com.zhonghui.mapper.ProductMapper;
import com.zhonghui.mapper.UserMapper;
import com.zhonghui.model.ProductComment;
import com.zhonghui.service.ProductCommentService;
import com.zhonghui.vo.PCommentListVo;
import com.zhonghui.vo.PCommentRecodeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (ProductComment)表服务实现类
 *
 * @author makejava
 * @since 2024-06-26 23:32:01
 */
@Service("productCommentService")
@Slf4j
public class ProductCommentServiceImpl implements ProductCommentService {
    @Resource
    private ProductCommentMapper productCommentDao;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private RedisUtils redisUtils;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public FwResult queryById(Long id) {
        ProductComment productComment = this.productCommentDao.queryById(id);
        return FwResult.ok(productComment);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public FwResult queryByPage(Long id, PageDto pageDto) {

        PageHelper.<ProductComment>startPage(pageDto.getCurrent(), pageDto.getSize());
        List<ProductComment> commentList = this.productCommentDao.queryAllByProductId(id);
        List<PCommentRecodeVo> commentVos = new ArrayList<>();
        for (ProductComment comment : commentList) {
            Long userId = comment.getUserId();
            String userName = userMapper.getUserById(userId).getNickname();
            PCommentRecodeVo commentVo = PCommentRecodeVo.builder()
                    .id(comment.getId())
                    .comment(comment.getComment())
                    .rate(comment.getRate())
                    .userId(userId)
                    .userName(userName)
                    .build();
            commentVos.add(commentVo);
        }
        PCommentListVo list = PCommentListVo.builder()
                .records(commentVos)
                .current(pageDto.getCurrent())
                .size(pageDto.getSize())
                .total((long)commentList.size())
                .build();
        return FwResult.ok(list);
    }

    /**
     * 新增数据
     *
     * @param productComment 实例对象
     * @return 实例对象
     */
    @Override
    public FwResult insert(ProductCommentDto productComment) {
        log.info("用户评价商品{}",productComment);
        String userId = redisUtils.get("userId");
        String comment = productComment.getComment();
        if (comment == null || comment.isEmpty()){
            comment = "用户未评价";
        }
        ProductComment productComment1 = ProductComment.builder()
                .userId(Long.valueOf(userId))
                .productId(productComment.getProductId())
                .comment(comment)
                .rate(productComment.getRate())
                .build();
        int i = this.productCommentDao.insert(productComment1);
        if (i > 0){
            return FwResult.okMsg("商品评价成功");
        }else {
            return FwResult.failedMsg("评价失败");
        }
    }

    /**
     * 修改数据
     *
     * @param productComment 实例对象
     * @return 实例对象
     */
    @Override
    public FwResult update(ProductComment productComment) {
        this.productCommentDao.update(productComment);
        return this.queryById(productComment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public FwResult deleteById(Long id) {
        int i = this.productCommentDao.deleteById(id);
        if (i > 0){
            return FwResult.okMsg("删除成功");
        }else {
            return FwResult.failedMsg("删除失败");
        }
    }
}
